import requests

url = "https://gitee.com/yao_huaiyu/projects"
url_login = 'https://gitee.com/login'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
}
# 请求体数据（网页上拷贝From data），字典，key：value，key：value
data = {
    'encrypt_key': 'password',
    'utf8': '✓',
    'authenticity_token': '41kAzlzxMzpY+5E7gpDQ5NbZITSoQdbKUaQSIP2nsk4=',
    'redirect_to_url': '',
    'user[login]': '2786849322@qq.com',
    'encrypt_data[user[password]]': 'ijg6tVy1OnBthf7sJ5ZHDa/dmCVUgJDemdh01Kf/Odn7LyyTLj6mblhtjypqDbaMQL/bBQVY4LCoIQ2WXv2URQ4N88uk5zTFrUaYNhJi9OGIIwKsW96GLWxHr7fEXPywFrKXSkVxilACSP+grq4aTWi8ZxgRcX+LhA5OuR8WxYc=',
    'user[remember_me]': '0',
}
# 利用requests库得到session，用session发送post请求，完成登录
session = requests.session()
r1 = session.post(url_login, headers=headers, data=data)
# 打印查看相应状态码
print(r1.status_code)
print(r1.content.decode())
# 使用session查看需要登录后才能看的url，不需要cookie，session会自动实现
url = "https://gitee.com/yao_huaiyu/projects"
r2 = session.get(url, headers=headers)
with open('session.html', 'w', encoding='utf-8') as f:
    f.write(r2.content.decode())
